package top.canyue.word_range_service.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import top.canyue.word_range.exception.BaseException;
import top.canyue.word_range.pojo.dto.EventDto;
import top.canyue.word_range.pojo.dto.LoginUser;
import top.canyue.word_range.pojo.dto.PageDTO;
import top.canyue.word_range.pojo.po.Event;
import top.canyue.word_range.pojo.query.EventQuery;
import top.canyue.word_range.pojo.vo.EventVo;
import top.canyue.word_range.result.Result;
import top.canyue.word_range_service.mapper.EventMapper;
import top.canyue.word_range_service.mapper.RelUserEventMapper;
import top.canyue.word_range_service.service.EventService;

import java.util.Date;

@Service
@RequiredArgsConstructor
public class EventServiceImpl extends ServiceImpl<EventMapper, Event> implements EventService {
    final EventMapper eventMapper;
    final RelUserEventMapper relUserEventMapper;

    @Override
    public Result<EventVo> addEvent(EventDto event) {
        Event eventPo = event.toPo();
        if (eventMapper.insert(eventPo) > 0) {
            return Result.success(eventPo.toVo());
        } else {
            throw new BaseException("添加失败");
        }
    }

    @Override
    public Result<EventVo> updateEvent(EventDto event) {
        Event eventPo = event.toPo();
        if (eventMapper.updateById(eventPo) > 0) {
            return Result.success(eventPo.toVo());
        } else {
            throw new BaseException("更新失败");
        }
    }

    @Override
    public Result<EventVo> getEventById(String id) {
        Event event = eventMapper.selectById(id);
        if (event != null) {
            return Result.success(event.toVo());
        } else {
            throw new BaseException("查询失败");
        }
    }

    @Override
    public Result<EventVo> deleteEventById(Long id) {
        if (eventMapper.deleteById(id) > 0) {
            return Result.success();
        } else {
            throw new BaseException("删除失败");
        }
    }

    // 获取列表
    @Override
    public PageDTO<EventVo> getEventList(EventQuery query) {
        String title = query.getTitle() == null ? "" : query.getTitle();
        Page<Event> page = query.toMpPage("end_date",false);
        Page<Event> eventPage = lambdaQuery()
                .like(!title.isEmpty(), Event::getTitle, title)
                .and(wrapper -> wrapper.isNull(Event::getEndDate).or().gt(Event::getEndDate, new Date()))
                .page(page);
        return PageDTO.of(eventPage, EventVo.class);
    }

    @Override
    public Result<EventVo> participateEvent(String eventId){
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder
                .getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        String loginUserId = loginUser.getUser().getId();
        if(relUserEventMapper.participateEvent(loginUserId,eventId) > 0){
            return Result.success();
        }else{
            throw new BaseException("参与活动失败");
        }
    }

    @Override
    public Result<EventVo> finishEvent(String eventId) {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken) SecurityContextHolder
                .getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();

        if(relUserEventMapper.finishEvent(loginUser.getUser().getId(),eventId) > 0){
            return Result.success();
        }else{
            throw new BaseException("完成活动失败");
        }
    }
}
